/*
 * @lc app=leetcode.cn id=706 lang=javascript
 *
 * [706] 设计哈希映射
 */

// @lc code=start

var MyHashMap = function() {
  this.BASE = 769;
  this.data = new Array(this.BASE).fill(0).map(() => []);
};

/**
 * @param {number} key
 * @param {number} value
 * @return {void}
 */
MyHashMap.prototype.hash = function(key) {
  return key % this.BASE;
};

/**
 * @param {number} key
 * @param {number} value
 * @return {void}
 */
MyHashMap.prototype.findIndex = function(key) {
  const h = this.hash(key);
  if (this.data[h] && this.data[h].length) {
    return this.data[h].findIndex(item => item[0] === key);
  }
  return -1;
};

/**
 * @param {number} key
 * @param {number} value
 * @return {void}
 */
MyHashMap.prototype.put = function(key, value) {
  const h = this.hash(key);
  let index = this.findIndex(key);
  if (index !== -1) {
    this.data[h] && (this.data[h][index][1] = value);
  } else {
    this.data[h] && this.data[h].push([key, value]);
  }
};

/**
 * @param {number} key
 * @return {number}
 */
MyHashMap.prototype.get = function(key) {
  const h = this.hash(key);
  let index = this.findIndex(key);
  if (!this.data[h]) {
    return -1;
  } else if (index !== -1) {
    return this.data[h][index][1];
  }
  return -1;
};

/**
 * @param {number} key
 * @return {void}
 */
MyHashMap.prototype.remove = function(key) {
  const h = this.hash(key);
  let index = this.findIndex(key);
  if (this.data[h] && index !== -1) {
    this.data[h].splice(index, 1);
  }
};

/**
 * Your MyHashMap object will be instantiated and called as such:
 * var obj = new MyHashMap()
 * obj.put(key,value)
 * var param_2 = obj.get(key)
 * obj.remove(key)
 */
// @lc code=end

